﻿using System;
using System.Data;
using System.Globalization;
using mojoPortal.Web;
using mojoPortal.Web.Framework;
using Resources;
using eStore.Business;
using eStore.Helpers;
using System.Collections;
using mojoPortal.Business;

namespace eStore.UI
{

    public partial class AdminDiscountsPage : mojoBasePage
    {
        private int pageId = -1;
        private int moduleId = -1;
        private Store store;
        private int pageNumber = 1;
        private const int pageSize = 15;
        private int totalPages = 1;
        private eStoreConfiguration config = new eStoreConfiguration();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserCanEditModule(moduleId))
            {
                SiteUtils.RedirectToAccessDeniedPage();
                return;
            }

            LoadSettings();
            PopulateLabels();
            PopulateControls();

        }

        private void PopulateControls()
        {
            BindGrid();

        }

        private void BindGrid()
        {
            if (store == null) { return; }

            using (IDataReader reader = Discount.GetPage(store.ModuleGuid, pageNumber, pageSize, out totalPages))
            {
                if (totalPages > 1)
                {

                    string pageUrl = SiteRoot + "/eStore/AdminDiscounts.aspx"
                        + "?pageid=" + pageId.ToInvariantString()
                        + "&amp;mid=" + moduleId.ToInvariantString()
                        + "&amp;pagenumber" + moduleId.ToInvariantString()
                        + "={0}";

                    pgrDiscounts.Visible = true;
                    pgrDiscounts.PageURLFormat = pageUrl;
                    pgrDiscounts.ShowFirstLast = true;
                    pgrDiscounts.CurrentIndex = pageNumber;
                    pgrDiscounts.PageSize = pageSize;
                    pgrDiscounts.PageCount = totalPages;

                }
                else
                {
                    pgrDiscounts.Visible = false;
                }

                grdDiscount.DataSource = reader;
                grdDiscount.PageIndex = pageNumber;
                grdDiscount.PageSize = pageSize;
                grdDiscount.DataBind();
            }

        }


        protected string BuildQueryString(string discountGuid)
        {

            string result = "?pageid=" + pageId.ToInvariantString()
                + "&mid=" + moduleId.ToInvariantString()
                + "&d=" + discountGuid;

            return result;

        }

        private void PopulateLabels()
        {
            lnkStoreMenu.Text = eStoreResources.StoreManagerHeading;
            lnkStoreMenu.NavigateUrl = SiteRoot + "/eStore/AdminDashboard.aspx?pageid="
                                       + pageId.ToString(CultureInfo.InvariantCulture)
                                       + "&mid=" + moduleId.ToString(CultureInfo.InvariantCulture);

            lnkCurrentPage.Text = eStoreResources.DiscountAdministration;
            lnkCurrentPage.NavigateUrl = Request.Url.ToString();

            Title = SiteUtils.FormatPageTitle(siteSettings, eStoreResources.DiscountAdministration);
            ltrTitle.Text = eStoreResources.DiscountAdministration;
            btnNewDiscount.Text = eStoreResources.NewDiscountLink;
            grdDiscount.Columns[0].HeaderText = @" ";
            grdDiscount.Columns[1].HeaderText = eStoreResources.DiscountDescriptionHeading;
            grdDiscount.Columns[2].HeaderText = eStoreResources.DiscountCodeHeading;


        }

        private void LoadSettings()
        {
            if (CurrentPage.ContainsModule(moduleId))
            {
                //store = StoreHelper.GetStore(siteSettings.SiteGuid, moduleId);
                store = StoreHelper.GetStore();
            }

            btnNewDiscount.PostBackUrl
                = SiteRoot
                + "/eStore/AdminDiscountEdit.aspx?pageid="
                + pageId.ToInvariantString()
                + "&mid=" + moduleId.ToInvariantString();

        }

        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", moduleId);
            pageNumber = WebUtils.ParseInt32FromQueryString("pagenumber", pageNumber);
        }

        private void LoadPanels()
        {
            Hashtable settings = ModuleSettings.GetModuleSettings(moduleId);
            config = new eStoreConfiguration(settings);
            LoadSideContent(false, false, true, true, 0);
        }

        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
        }

        override protected void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();

            base.OnInit(e);
            Load += Page_Load;
        }
        #endregion
    }
}

